package com.xframe.xdal.core.util;


import cn.hutool.core.date.DateUtil;
import com.xframe.xdal.core.util.redis.RedisAccess;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zjj
 * @create 2024/3/28 11:43
 */
public class BillNoCreateUtil {

    public static final String PADSTR = "0";


    public  static  String getBillNo(String key, Integer size) {
        String dateStr  = DateUtil.format(new Date(),"yyyyMMdd");
        Long billNo = RedisAccess.getSetCmd().incrBy(key + dateStr,1);
        RedisAccess.getKeyCmd().expire(key + dateStr,24 * 60 * 60);

        return key + dateStr + StringUtils.leftPad(billNo.toString(), size, PADSTR);
    }
    /**
     * 获取国内装箱单号
     * 前缀BX+年月日（8位）+ 流水号
     */
    public  static  String getBoxNoByGN(String key, Integer size) {
        String dateStr  = DateUtil.format(new Date(),"yyyyMMdd");
        Long billNo = RedisAccess.getSetCmd().incrBy(key + dateStr,1);
        RedisAccess.getKeyCmd().expire(key + dateStr,24 * 60 * 60);

        return key + dateStr + StringUtils.leftPad(billNo.toString(), size, PADSTR);
    }

    /**
     * 获取国外装箱单号
     * 国家代码 + 订单类型前缀 + 流水号
     */
    public  static  String getBoxNoByGW(String countryCode, String orderType,Integer size) {

        String orderTypePre = "";
        Map<String, String> orderTypeMapping = new HashMap<>();
        orderTypeMapping.put("A", "GO");
        orderTypeMapping.put("B", "EO");
        orderTypeMapping.put("C", "KH");
        orderTypeMapping.put("D", "SB");
        orderTypeMapping.put("E", "MF");
        orderTypePre = orderTypeMapping.getOrDefault(orderType, "");
        String dateStr  = DateUtil.format(new Date(),"yyyyMMdd");

        Long billNo = RedisAccess.getSetCmd().incrBy(countryCode + orderTypePre + dateStr,1);
        RedisAccess.getKeyCmd().expire(countryCode + orderTypePre + dateStr,24 * 60 * 60);
        return countryCode + orderTypePre + StringUtils.leftPad(billNo.toString(), size, PADSTR);
    }

    /**
     * 获取委托单编码
     * 国家代码+经销商编码 + 2位年份 + '-' + 3位序号 + 发运类型 + '-' + 月+日(2位)
     * 如厄瓜多尔空运委托单编码 EC20079422023-01A-1219;
     */
    public static String getPackagingCommissionNo(String countryCode,String dealerCode,String transportation, Integer size){
        String dateStr  = DateUtil.format(new Date(),"MMdd");
        String yStr = DateUtil.format(new Date(),"yyyy");
        String key = countryCode + dealerCode + yStr + dateStr + transportation;
        Long billNo = RedisAccess.getSetCmd().incrBy(key,1);
        RedisAccess.getKeyCmd().expire(key,24 * 60 * 60);
        return countryCode + dealerCode + yStr + "-" + StringUtils.leftPad(billNo.toString(), size, PADSTR) + transportation + "-" +dateStr;
    }

    /**
     * 获取备货单号 BH+8位年月日+4位流水号
     */
    public static String getStockingNo(String key, Integer size) {

        String dateStr  = DateUtil.format(new Date(),"yyyyMMdd");
        Long billNo = RedisAccess.getSetCmd().incrBy(key + dateStr,1);
        RedisAccess.getKeyCmd().expire(key + dateStr,24 * 60 * 60);
        return key + dateStr + StringUtils.leftPad(billNo.toString(), size, PADSTR);
    }

    /**
     * 获取调拨单号 分库编码+BD+8位年月日+3位流水号
     */
    public static String getAllocatioNo(String key, Integer size){

        String dateStr  = DateUtil.format(new Date(),"yyyyMMdd");
        Long billNo = RedisAccess.getSetCmd().incrBy(key + dateStr,1);
        RedisAccess.getKeyCmd().expire(key + dateStr,24 * 60 * 60);
        return key + dateStr + StringUtils.leftPad(billNo.toString(), size, PADSTR);
    }

    /**
     * 获取盘亏出库单号 PK + +  4为年 + 2位月  + 2为日  + 3为流水号
     */
    public static String getLostBillNo(String key, Integer size){

        String dateStr  = DateUtil.format(new Date(),"yyyyMMdd");
        Long billNo = RedisAccess.getSetCmd().incrBy(key + dateStr,1);
        RedisAccess.getKeyCmd().expire(key + dateStr,24 * 60 * 60);
        return key + dateStr + StringUtils.leftPad(billNo.toString(), size, PADSTR);

    }

    /**
     * 获取盘盈入库单号 PY +  4为年 + 2位月  + 2为日  + 3为流水号
     */
    public static String getInBillNo(String key, Integer size){

        String dateStr  = DateUtil.format(new Date(),"yyyyMMdd");
        Long billNo = RedisAccess.getSetCmd().incrBy(key + dateStr,1);
        RedisAccess.getKeyCmd().expire(key + dateStr,24 * 60 * 60);
        return key + dateStr + StringUtils.leftPad(billNo.toString(), size, PADSTR);
    }


}
